package com.guli.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.base.exception.GuliException;
import com.guli.ucenter.entity.Member;
import com.guli.ucenter.entity.vo.LoginVo;
import com.guli.ucenter.entity.vo.RegisterVo;
import com.guli.ucenter.mapper.MemberMapper;
import com.guli.ucenter.service.MemberService;
import com.guli.utils.JwtUtils;
import com.guli.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-10-02
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) throw new GuliException(20001, "传入vo有误");

        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));

        if (member == null) throw new GuliException(20001, "账号不存在");

        if(!MD5.encrypt(password).equals(member.getPassword())) throw new GuliException(20001, "密码错误");

        if (member.getIsDisabled()) throw new GuliException(20001, "账号被锁定");

        return JwtUtils.getJwtToken(member.getId(), member.getNickname());

    }

    @Override
    public void register(RegisterVo registerVo) {

        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"传入vo有误");
        }

        String mobileCode = redisTemplate.opsForValue().get(mobile);

        if (!code.equals(mobileCode)) throw new GuliException(20001, "验证码错误");

        Integer count = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", mobile));
        if (count > 0) throw new GuliException(20001, "账号已存在");

        // 数据合格注册成功放入数据库
        //添加注册信息到数据库
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://localhost:9001/img/2022/09/24/c09daa6dd93c4e42a7d400e5524253e4.jpeg");
        baseMapper.insert(member);

    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }


}
